﻿using System;
using System.Collections.Generic;
using System.Text;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.IBatisRepository;
using OneMG.Phoenix.Respository;
using System.Data;
using OneMG.Phoenix.Entity;

namespace OneMG.Phoenix.Service
{
    public class CategoryService : ICategoryService
    {
		 ICategory category = new CategoryRepository();

         public System.Int32 AddCategory(CategoryModel model)
        {
            ECategory entity = ConvertModelToEntity(model);

            return category.AddCategory(entity);
        }

		public void UpdateByCategoryID (CategoryModel model)
		{
            ECategory entity = ConvertModelToEntity(model);

            category.UpdateByCategoryID(entity);

		}

		public void DeleteByCategoryID (System.Int32 categoryID)
		{
		
            category.DeleteByCategoryID(categoryID);

		}
		 
        public CategoryModel GetByCategoryID (System.Int32 categoryID)
        {
        
			ECategory entity = category.GetByCategoryID(categoryID);
			
			if(entity ==null)
			return null;

            CategoryModel model = ConvertEntityToModel(entity);

            return model;
        }

        public IList<CategoryModel> GetByParentID(System.Int32 parentID)
        {
            IList<ECategory> listEntity  = category.GetByParentID(parentID);

            if (listEntity == null)
                return null;

            IList<CategoryModel> listModel = new List<CategoryModel>();

            foreach (var item in listEntity)
            {
                CategoryModel model = ConvertEntityToModel(item);

                listModel.Add(model);
            }
            return listModel;
        }

        public CategoryModel ConvertEntityToModel(ECategory entity)
        {
            if (entity == null)
            {
                return null;
            }
            CategoryModel model = new CategoryModel();
            model.CategoryID = entity.CategoryID;
            model.CategoryName = entity.CategoryName;
            model.IsLeaf = entity.IsLeaf;
            model.ParentID = entity.ParentID;
            model.CateAleph = entity.CateAleph;
            model.CateStatus = entity.CateStatus;
            model.CateSort = entity.CateSort;
            model.LinkUrl = entity.LinkUrl;
            model.Photo = entity.Photo;
            model.CreateUser = entity.CreateUser;
            model.UpdateUser = entity.UpdateUser;
            model.CreateTime = entity.CreateTime;
            model.UpdateTime = entity.UpdateTime;
            return model;
        }

        public ECategory ConvertModelToEntity(CategoryModel model)
        {
            if (model == null)
            {
                return null;
            }
            ECategory entity = new ECategory();
            entity.CategoryID = model.CategoryID;
            entity.CategoryName = model.CategoryName;
            entity.IsLeaf = model.IsLeaf;
            entity.ParentID = model.ParentID;
            entity.CateAleph = model.CateAleph;
            entity.CateStatus = model.CateStatus;
            entity.CateSort = model.CateSort;
            entity.LinkUrl = model.LinkUrl;
            entity.Photo = model.Photo;
            entity.CreateUser = model.CreateUser;
            entity.UpdateUser = model.UpdateUser;
            entity.CreateTime = model.CreateTime;
            entity.UpdateTime = model.UpdateTime;
            return entity;
        }
        
    }
}